package down.xiaoheihe.work.Controller;

import down.xiaoheihe.work.Entity.TProduct;
import down.xiaoheihe.work.Entity.TProductExample;
import down.xiaoheihe.work.Entity.Timages;
import down.xiaoheihe.work.Entity.TimagesExample;
import down.xiaoheihe.work.Mapper.TProductMapper;
import down.xiaoheihe.work.Mapper.TimagesMapper;
import down.xiaoheihe.work.Mapper.UserMapper;
import down.xiaoheihe.work.Response.ESproduct;
import down.xiaoheihe.work.utils.ElasticUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.*;

@RestController
@MapperScan("down.xiaoheihe.work.Mapper")
@CrossOrigin
public class ElasticSearchController {
    @Autowired
    UserMapper tUserMapper;
    @Autowired
    TProductMapper tProductMapper;
    @Autowired
    TimagesMapper imagesMapper;
    @Autowired
    ElasticUtils utils;
    /**
     * 发布商品
     *
     * @param title       商品标题
     * @param description 商品描述
     * @param price       商品价格
     * @param deposit     商品租金
     */
    @PostMapping("/shangpinfabu")
    public Map<String, Object> 发布商品(
            @RequestParam String title,
            @RequestParam String description,
            @RequestParam String price,
            @RequestParam String deposit) {
        Subject subject = SecurityUtils.getSubject();
        Integer id = (Integer) subject.getPrincipal();
        TProduct tProduct = new TProduct();
        tProduct.setTitle(title);
        tProduct.setDescription(description);
        tProduct.setPrice(Long.valueOf(price));
        tProduct.setGuarentee(Integer.valueOf(deposit));
        tProduct.setStatus(1);
        tProduct.setCreaterId(id);
        tProductMapper.insert(tProduct);
        Integer productId = tProduct.getId();
        String userName = tUserMapper.selectByPrimaryKey(id).getUsername();
        Map<String, Object> map = new HashMap<>();
        map.put("id", productId);
        map.put("name", userName);
        return map;
    }
    /**
     * 在ES数据库中创建商品文档
     * @param productId 商品id
     */
    @PostMapping("/uptoes")
    public @ResponseBody String upLoadToES(@RequestParam Integer productId) throws IOException {
        TimagesExample example1 = new TimagesExample();
        example1.createCriteria().andProductIdEqualTo(productId);
        List<Timages> images = imagesMapper.selectByExample(example1);
        TProduct tProduct = tProductMapper.selectByPrimaryKey(productId);
        utils.创建索引(tProduct, images);
        return "OK";
    }
    /**
     * 获取8个随机的商品
     */
    @GetMapping("/toitems")
    public List<ESproduct> getItems2() throws IOException {
        List<ESproduct> result = new ArrayList<>();
        TProductExample tproductExample = new TProductExample();
        tproductExample.createCriteria().andStatusEqualTo(1);
        List<TProduct> tProducts = tProductMapper.selectByExample(tproductExample);
        for (TProduct tProduct : tProducts) {
            ESproduct a = new ESproduct(tProduct);
            TimagesExample b = new TimagesExample();
            b.createCriteria().andProductIdEqualTo(tProduct.getId());
            List<Timages> timages = imagesMapper.selectByExample(b);
            a.setTimages(timages);
            result.add(a);
        }
        List randomList = getRandomList(result, 8);
        return randomList;
    }
    /**
     * 从list中随机抽取不重复的元素
     * @param paramList 要抽取的list
     * @param count 要抽取的个数
     * @return
     */
    public static List getRandomList(List<ESproduct> paramList,int count){
        if(paramList.size()<count){
            return paramList;
        }
        Random random=new Random();
        List<Integer> tempList=new ArrayList<>();
        List<ESproduct> newList=new ArrayList<>();
        int temp=0;
        for(int i=0;i<count;i++){
            temp=random.nextInt(paramList.size());
            if(!tempList.contains(temp)){
                tempList.add(temp);
                newList.add(paramList.get(temp));
            }
            else{
                i--;
            }
        }
        return newList;
    }
}
